//
//  SendMessageController.m
//  DYChats
//
//  Created by 杨德勇 on 2016/9/12.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "SendMessageController.h"
#import "testViewController.h"
@interface SendMessageController ()<GCDAsyncUdpSocketDelegate>

@end

@implementation SendMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"发送";
    self.view.backgroundColor = [UIColor yellowColor];
    [self setupNav];
    [self setupUdpSocket];
    [self test];
}

-(void)test{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(200, 200, 100, 100)];
    btn.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)jump{
    testViewController *testvc = [[testViewController alloc]init];
    [self.navigationController pushViewController:testvc animated:YES];
}

-(void)setupNav{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送消息" style:UIBarButtonItemStylePlain target:self action:@selector(sendMessage)];
    
    
}
-(void)sendMessage{
    NSString *message = @"hello UDP";
    NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
    NSString *host = @"224.0.0.1";
    uint16_t port = 4333 ;
    [self.udpSocket sendData:data toHost:host port:port withTimeout:60 tag:100];
    
}
-(void)setupUdpSocket{
    
    dispatch_queue_t dqueue = dispatch_queue_create("client udp socket", nil);
    
    self.udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dqueue];
    [self.udpSocket enableBroadcast:YES error:nil];
//    [self.udpSocket bindToPort:8085 error:nil];
    [self.udpSocket bindToPort:4333 error:nil];
    [self.udpSocket receiveOnce:nil];
}
#
#pragma mark -
#pragma mark -udpSocketDelegate
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
    if (tag == 100) {
        NSLog(@"成功发送消息");
    }
}
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
   
        NSLog(@"没有成功发送消息，tag:%ld,error:%@",tag,error);
    
}
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext{
    NSString *ip = [GCDAsyncUdpSocket hostFromAddress:address];
    uint16_t port = [GCDAsyncUdpSocket portFromAddress:address];
    NSString *msg = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
//    NSLog(@"ydy - 收到来自：%@:%d 的消息:%@",ip,port,msg);
    [sock receiveOnce:nil];
    
}

-(void)sendbacktoHost:(NSString *)ip port:(uint16_t )port withMessage:(NSString *)message{
    if (message.length == 0) {
        message = @"我再发一条消息";
    }
    NSData *msgdata = [message dataUsingEncoding:NSUTF8StringEncoding];
    [self.udpSocket sendData:msgdata toHost:ip port:port withTimeout:60 tag:200];
}
-(void)dealloc{
    NSLog(@"%s",__func__ );
    [self.udpSocket close];
    self.udpSocket = nil;
}
@end
